//配置axios的基础路径
axios.defaults.baseURL = "http://localhost:8080/";
//配置Vue的全局属性用来获取axios对象
Vue.prototype.$http = axios;

//======================路由的前端拦截器【拦截不到后端的请求】====================//
let url = location.href;
if (url.indexOf("login.html") == -1
    && url.indexOf("register.html") == -1
    && url.indexOf("callback.html") == -1
    && url.indexOf("binder.html") == -1
){
    let loginInfo = localStorage.getItem('loginInfo');
    if (!loginInfo){
        location.href = "/login.html";
    }
}



//======================axios的前置拦截器【拦截调用后端的请求====================//
axios.interceptors.request.use(res=>{


    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
})
//======================axios的前置拦截器【拦截调用后端的请求】====================//


//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "noLogin" === res.data.message) {
        localStorage.removeItem("token");
        localStorage.removeItem("loginInfo");
        location.href = "/index.html"
    }
    return res;
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//


//封装公用方法 - 将字符串的参数解析为json对象
function parseUrlParams2Obj(url) {
    let paramStr = url.substring(url.indexOf("?")+1);//xxx?code=xxx&state=1
    let paramArr = paramStr.split("&");
    let paramObj = {};
    for(let i = 0;i<paramArr.length;i++){
        let paramTemp = paramArr[i];
        let paramName = paramTemp.split("=")[0];
        let paramValue= paramTemp.split("=")[1];
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}